package com.example.neo4jdemo.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.text.csv.CsvData;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvRow;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.example.neo4jdemo.entity.BitEntity;
import com.example.neo4jdemo.entity.UserEntity;
import com.example.neo4jdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/addUser")
    public String addUserNode(){
        UserEntity userEntity = new UserEntity();
        userEntity.setName("张三");
        userEntity.setAge(20);
        userService.addUser(userEntity);
        return "success";
    }
    @PostMapping("/addData")
    public String addDate(String aaa){
        CsvReader reader = CsvUtil.getReader();
//从文件中读取CSV数据
        CsvData data = reader.read(FileUtil.file("d:/data.csv"));
        List<CsvRow> rows = data.getRows();
//遍历行
        List<BitEntity> bitEntityList = new ArrayList<>();
        for(int i=1;i<rows.size();i++){
            BitEntity bitEntity = new BitEntity();
            bitEntity.setHash(rows.get(i).get(0));
            bitEntity.setFrom(rows.get(i).get(1));
            bitEntity.setTo(rows.get(i).get(2));
            bitEntity.setAmount(rows.get(i).get(3));
            bitEntity.setUnit(rows.get(i).get(4));
            bitEntityList.add(bitEntity);
        }
        userService.saveAll(bitEntityList);
        System.out.println(bitEntityList.get(0).getHash()+"生活");
        return bitEntityList.get(0).getHash();

    }
}
